<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <!-- 左侧部分 -->
    <el-col>
      <i class="el-icon-office-building"></i>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <!-- 右侧部分 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>
          <span>{{ treeNode.manager }}</span>
        </el-col>
        <el-col>
          <!-- element-ui 下拉菜单 -->
          <el-dropdown @command="operateDepts">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">
                编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 点击操作下的新增 编辑 删除 按钮时触发
    operateDepts(command) {
      if (command === 'add') {
        this.$emit('addDepts', this.treeNode)
      } else if (command === 'edit') {
        this.$emit('editDepts', this.treeNode)
      } else {
        this.$confirm('确认删除？')
          .then(() => {
            console.log(this)
            return delDepartments(this.treeNode.id)
          })
          .then(() => {
            this.$emit('delDeparts')
            this.$message.success('删除成功')
          })
      }
    }
  }
}
</script>

<style></style>
